/**
 * 
 */
package org.ejc.statistics.charts;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.jfree.data.general.DefaultPieDataset;

import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;

/**
 * @author Brian Boyle
 *
 */
public class PieChart implements DatasetProducer, Serializable {

	private String[] categories;
	private int[] values;
	
	/**
	 * 
	 */
	public PieChart(Map map) 
	{
		categories = new String[map.size()];
		values = new int[map.size()];
		Iterator iter = map.keySet().iterator();
		int count = 0;
		while(iter.hasNext()){
			String key = iter.next().toString();
			categories[count] = key;
			Integer currentValue = (Integer) map.get(key);
			values[count] = currentValue.intValue();
			count++;
		}
	}

	/* (non-Javadoc)
	 * @see de.laures.cewolf.DatasetProducer#produceDataset(java.util.Map)
	 */
	public Object produceDataset(Map arg0) throws DatasetProduceException {
		DefaultPieDataset pieDataSet = new DefaultPieDataset();
		for (int series = 0; series < categories.length; series++) {
			int currentValue = values[series];
			pieDataSet.setValue(categories[series], currentValue);
		}
		return pieDataSet;
	}

	/* (non-Javadoc)
	 * @see de.laures.cewolf.DatasetProducer#hasExpired(java.util.Map, java.util.Date)
	 */
	public boolean hasExpired(Map arg0, Date since) {
		return (System.currentTimeMillis() - since.getTime())  > 5000;
	}

	/* (non-Javadoc)
	 * @see de.laures.cewolf.DatasetProducer#getProducerId()
	 */
	public String getProducerId() {
		return "PieChart Producer";
	}

}
